package com.timothy.android;

import java.util.ArrayList;
import java.util.List;

import com.timothy.question.Choice;
import com.timothy.question.STTwoButtonAlpha;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LayoutA extends BaseQuestionActivity{
	private static final String LOG_TAG = "LayoutA";
	
	private LinearLayout layoutA;
	private TextView tv_msg = null;
	private Button btnA = null;
	private Button btnB = null;	
	private Button btnPre = null;
	private Button btnNext = null;
	private Drawable btnDrawable = null;
	
	STTwoButtonAlpha sTTwoButtonAlpha = null;
	
	MyCount mc;
	private boolean countDownMark;
	private int countDownTime;
	private int countDownInterval;
	
	public void initialSetting(){
		
		//layoutA
		layoutA = (LinearLayout) this.findViewById(R.id.layoutA);
		layoutA.setBackgroundColor(LayoutAConfig.LAYOUTA_BG_COLOR);
		layoutA.setPadding(LayoutAConfig.LAYOUTA_PADDING, LayoutAConfig.LAYOUTA_PADDING, 
				LayoutAConfig.LAYOUTA_PADDING, LayoutAConfig.LAYOUTA_PADDING);
		
		//tv_msg 
		tv_msg = (TextView) this.findViewById(R.id.promotMsg);
		tv_msg.setHeight(LayoutAConfig.TV_MSG_HEIGHT);
		tv_msg.setTextSize(LayoutAConfig.TV_MSG_TEXTSIZE);
		tv_msg.setBackgroundColor(LayoutAConfig.TV_MSG_BG_COLOR);
		tv_msg.setTextColor(LayoutAConfig.TV_MSG_TEXTCOLOR);
		
		btnDrawable = this.getResources().getDrawable(R.drawable.button);
		
		//设置ButtonA
		btnA = (Button) this.findViewById(R.id.btnA);
		btnA.setHeight(LayoutAConfig.BUTTONA_HEIGHT);
		btnA.setTextColor(LayoutAConfig.BUTTONA_TEXTCOLOR);
		btnA.setTextSize(LayoutAConfig.BUTTONA_TEXTSIZE);
		btnA.setBackgroundDrawable(btnDrawable); 
		
		//设置ButtonB
		btnB = (Button) this.findViewById(R.id.btnB);
		btnB.setHeight(LayoutAConfig.BUTTONB_HEIGHT);
		btnB.setTextColor(LayoutAConfig.BUTTONB_TEXTCOLOR);
		btnB.setTextSize(LayoutAConfig.BUTTONB_TEXTSIZE);
		btnB.setBackgroundDrawable(btnDrawable); 
		
		//countDown
		countDownMark = LayoutAConfig.COUNT_DOWN_MARK;
		countDownTime = LayoutAConfig.COUNT_DOWN_TIME; 
		countDownInterval = LayoutAConfig.COUNT_DOWN_INTERVAL;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.layouta);
		
		initialSetting();
		
		//mock information
		Bundle bundle = this.getIntent().getExtras();
		String question = bundle.getString("question");
		String choice1 = bundle.getString("choice1");
		String choice2 = bundle.getString("choice2");
		
		sTTwoButtonAlpha = new STTwoButtonAlpha();
		List<Choice> mockChoices = new ArrayList<Choice>();
		sTTwoButtonAlpha.setQuestionName(question);
		mockChoices.add(new Choice("Y",choice1));
		mockChoices.add(new Choice("N",choice2));
		sTTwoButtonAlpha.setChoices(mockChoices);
		
		tv_msg.setText(sTTwoButtonAlpha.getQuestionName());

		List<Choice> choices = sTTwoButtonAlpha.getChoices();
		btnA.setText(choices.get(0).getContent());
		btnB.setText(choices.get(1).getContent());
        
				
		btnA.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundColor(LayoutAConfig.BUTTONA_BG_COLOR);
				btnB.setBackgroundDrawable(btnDrawable); 
			}
		});
		
		btnB.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundDrawable(btnDrawable);
				btnB.setBackgroundColor(LayoutAConfig.BUTTONB_BG_COLOR);
			}
		});
		
		//往前或往后翻阅题目
		btnPre = (Button) this.findViewById(R.id.btnPre);
		btnPre.setEnabled(false);
		btnPre.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.setClass(LayoutA.this,LayoutB.class);
			    startActivity(intent);
			}
		});
		
		btnNext = (Button) this.findViewById(R.id.btnNext);
		btnNext.setEnabled(false);
		btnNext.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.setClass(LayoutA.this,LayoutB.class);
			    startActivity(intent);
			}
		});
		
		if(countDownMark){
			List<Button> btnList = new ArrayList<Button>(); 
			btnList.add(btnA);
			btnList.add(btnB);
	        mc = new MyCount(countDownTime * 1000,countDownInterval * 1000,btnList,this.getBaseContext());  
	        mc.start();			
		}

	}

	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	
}
